﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;


namespace inventory.Controls
{
    public class gridStatementSum : common.controls.baseDataGridView 
    {
        public gridStatementSum()
        {
            myItemSource.DataSource = DataAccess.DataCache.myMasterDS.Item;
            this.AutoGenerateColumns = false;
            this.myAutoFitColumn = ColumnNames.itemName.ToString();
            this.LockEdit(true);
        }
        BindingSource myItemSource = new BindingSource();
        public enum ColumnNames
        {
            code, itemCode, itemSubCode, itemName, itemUnit,
            qtyNeed, qtyNeedReturn, qtyMove, qtyOut, qtyOutReturn, qtyNeedRemain,
            qtyOutSale, qtyOutClearance, qtyOutRemain
        };
        public void Init(data.tmpDS.statementSumDataTable tbl)
        {
            Init(tbl, null);
        }
        public void Init(data.tmpDS.statementSumDataTable tbl,string filter)
        {
            // 
            // code
            // 
            common.controls.DataGridViewTextBoxColumnExt code = new common.controls.DataGridViewTextBoxColumnExt();
            code.Uppercase = true;
            code.DataPropertyName = tbl.statementIdColumn.ColumnName;
            code.HeaderText = "Mã lệnh";
            code.Name = ColumnNames.code.ToString();
            code.Width = 100;

            // 
            // itemCode
            // 
            common.controls.DataGridViewTextBoxColumnExt itemCode = new common.controls.DataGridViewTextBoxColumnExt();
            itemCode.Uppercase = true;
            itemCode.DataPropertyName = tbl.itemCodeColumn.ColumnName;
            itemCode.HeaderText = "Mã hàng";
            itemCode.Name = ColumnNames.itemCode.ToString();
            itemCode.Width = 100;

            // 
            // itemSubCode
            // 
            common.controls.DataGridViewTextBoxColumnExt itemSubCode = new common.controls.DataGridViewTextBoxColumnExt();
            itemSubCode.Uppercase = true;
            itemSubCode.DataPropertyName = tbl.itemSubCodeColumn.ColumnName;
            itemSubCode.HeaderText = "";
            itemSubCode.Name = ColumnNames.itemSubCode.ToString();
            itemSubCode.Width = 35;

            // 
            // itemName
            // 
            DataGridViewComboBoxColumn itemName = new DataGridViewComboBoxColumn();
            itemName.DataSource = this.myItemSource;
            itemName.DataPropertyName = tbl.itemCodeColumn.ColumnName;
            itemName.DisplayMember = DataAccess.DataCache.myMasterDS.Item.nameColumn.ColumnName;
            itemName.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            itemName.HeaderText = "Diễn giải";
            itemName.Name = ColumnNames.itemName.ToString();
            itemName.ReadOnly = true;

            itemName.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            itemName.Width = 370;
            itemName.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            itemName.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;

            // 
            // unit
            // 
            DataGridViewComboBoxColumn itemUnit = new DataGridViewComboBoxColumn();
            itemUnit.DataSource = this.myItemSource;
            itemUnit.DataPropertyName = tbl.itemCodeColumn.ColumnName;
            itemUnit.DisplayMember = DataAccess.DataCache.myMasterDS.Item.unitColumn.ColumnName;
            itemUnit.ValueMember = DataAccess.DataCache.myMasterDS.Item.itemCodeColumn.ColumnName;
            itemUnit.HeaderText = "ĐVT";
            itemUnit.Name = ColumnNames.itemUnit.ToString();
            itemUnit.ReadOnly = true;
            itemUnit.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            itemUnit.DefaultCellStyle.BackColor = CommonTypes.Settings.sysColorDisableBG;
            itemUnit.DefaultCellStyle.ForeColor = CommonTypes.Settings.sysColorDisableFG;
            itemUnit.Width = 60;
            // 
            // Qty
            // 

            DataGridViewTextBoxColumn qtyStatement = new DataGridViewTextBoxColumn();
            qtyStatement.DataPropertyName = tbl.qtyNeedColumn.ColumnName;
            qtyStatement.HeaderText = "Lệnh.xuất";
            qtyStatement.Name = ColumnNames.qtyNeed.ToString();
            qtyStatement.DefaultCellStyle = application.SysLibs.GetViewCelStyleQTY();
            qtyStatement.Width = 80;


            DataGridViewTextBoxColumn qtyNeedResturn = new DataGridViewTextBoxColumn();
            qtyNeedResturn.DataPropertyName = tbl.qtyNeedReturnColumn.ColumnName;
            qtyNeedResturn.HeaderText = "Thu.hồi";
            qtyNeedResturn.Name = ColumnNames.qtyNeedReturn.ToString();
            qtyNeedResturn.DefaultCellStyle = application.SysLibs.GetViewCelStyleQTY();
            qtyNeedResturn.Width = 80;


            DataGridViewTextBoxColumn qtyMove = new DataGridViewTextBoxColumn();
            qtyMove.DataPropertyName = tbl.qtyOutMoveColumn.ColumnName;
            qtyMove.HeaderText = "Đã.chuyển";
            qtyMove.Name = ColumnNames.qtyMove.ToString();
            qtyMove.DefaultCellStyle = application.SysLibs.GetViewCelStyleQTY(); 
            qtyMove.Width = 85;
            
            
            DataGridViewTextBoxColumn qtyOut = new DataGridViewTextBoxColumn();
            qtyOut.DataPropertyName = tbl.qtyOutColumn.ColumnName;
            qtyOut.HeaderText = "Đã.xuất";
            qtyOut.Name = ColumnNames.qtyOut.ToString();
            qtyOut.DefaultCellStyle = application.SysLibs.GetViewCelStyleQTY();
            qtyOut.Width = 80;

            DataGridViewTextBoxColumn qtyReturn = new DataGridViewTextBoxColumn();
            qtyReturn.DataPropertyName = tbl.qtyOutReturnColumn.ColumnName;
            qtyReturn.HeaderText = "Nhập.lại";
            qtyReturn.Name = ColumnNames.qtyOutReturn.ToString();
            qtyReturn.DefaultCellStyle = application.SysLibs.GetViewCelStyleQTY();
            qtyReturn.Width = 80;

            DataGridViewTextBoxColumn qtyNeedRemain = new DataGridViewTextBoxColumn();
            qtyNeedRemain.DataPropertyName = tbl.qtyNeedRemainColumn.ColumnName;
            qtyNeedRemain.HeaderText = "Chưa.xuất";
            qtyNeedRemain.Name = ColumnNames.qtyNeedRemain.ToString();
            qtyNeedRemain.DefaultCellStyle = application.SysLibs.GetViewCelStyleQTY(); 
            qtyNeedRemain.Width = 80;

            DataGridViewTextBoxColumn qtyOutSale = new DataGridViewTextBoxColumn();
            qtyOutSale.DataPropertyName = tbl.qtyOutSaleColumn.ColumnName;
            qtyOutSale.HeaderText = "Đã bán";
            qtyOutSale.Name = ColumnNames.qtyOutSale.ToString();
            qtyOutSale.DefaultCellStyle = application.SysLibs.GetViewCelStyleQTY(); 
            qtyOutSale.Width = 80;
            qtyOutSale.Visible = false;

            DataGridViewTextBoxColumn qtyOutClearance = new DataGridViewTextBoxColumn();
            qtyOutClearance.DataPropertyName = tbl.qtyOutClearaneColumn.ColumnName;
            qtyOutClearance.HeaderText = "Khác";
            qtyOutClearance.Name = ColumnNames.qtyOutClearance.ToString();
            qtyOutClearance.DefaultCellStyle = application.SysLibs.GetViewCelStyleQTY();
            qtyOutClearance.Width = 80;
            qtyOutClearance.Visible = false;

            DataGridViewTextBoxColumn qtyOutRemain = new DataGridViewTextBoxColumn();
            qtyOutRemain.DataPropertyName = tbl.qtyOutRemainColumn.ColumnName;
            qtyOutRemain.HeaderText = "Còn.lại";
            qtyOutRemain.Name = ColumnNames.qtyOutRemain.ToString();
            qtyOutRemain.DefaultCellStyle = application.SysLibs.GetViewCelStyleQTY(); 
            qtyOutRemain.Width = 80;
            qtyOutRemain.Visible = false;
            // 
            // myDetailGrid
            // 
            this.Columns.Clear();
            this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[]  { 
                code, itemCode, itemSubCode, itemName, itemUnit, qtyStatement, qtyNeedResturn,qtyMove, qtyOut, qtyReturn, qtyNeedRemain, 
                qtyOutSale, qtyOutClearance, qtyOutRemain }
            );

            DataView dataView = new DataView(tbl);
            dataView.RowFilter = (filter == null ? "" : filter);
            this.DataSource = dataView;
            UpdateItemList();
            AutoArrange();
        }
        public new int RowCount
        {
            get
            {
                if (this.DataSource == null) return 0;
                return ((DataView)this.DataSource).Count;
            }
        }
        private void UpdateItemList()
        {
            DataView dataView = (DataView)this.DataSource;
            for (int idx = 0; idx < dataView.Count; idx++)
            {
                DataAccess.DataCache.FindITEM(((data.tmpDS.statementSumRow)(dataView[idx] as DataRowView).Row).itemCode);
            }
        }

    }
}
